package com.leyou.user.web;


import com.leyou.auth.dto.UserDetail;
import com.leyou.auth.utils.UserContext;
import com.leyou.item.dto.UserDTO;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/info")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 判断信息是否存在
     * @param date
     * @param type
     * @return
     */
    @GetMapping("/exists/{data}/{type}")
    public ResponseEntity<Boolean> isExistsInfo(
            @PathVariable("data") String date,
            @PathVariable("type") Integer type
    ) {
        this.userService.isExistsInfo(date, type);
        return ResponseEntity.ok().build();
    }





    /**
     * 根据用户名和密码查询
     * @param username
     * @param password
     * @return
     */
    @GetMapping
    public ResponseEntity<UserDTO> queryUserByUsernamePassword(
            @RequestParam("username") String username,
            @RequestParam("password") String password
    ) {
        return ResponseEntity.ok(userService.queryUserByUsernamePassword(username, password));
    }


    /**
     * 用户注册
     * @param
     * @param code
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> loginUser(
            @RequestParam ("username") String username,
            @RequestParam ("password") String password,
            @RequestParam ("phone") String phone,
            @RequestParam("code") String code) {

        this.userService.loginUser(username, password, phone, code);
        return ResponseEntity.ok().build();
    }




    /**
     * 发送短信验证码
     * @param phone
     * @return
     */
    @PostMapping("/code")
    public ResponseEntity<Void> codeSms(@RequestParam("phone") String phone) {
        this.userService.codeSms(phone);
        return ResponseEntity.ok().build();
    }


//    /**
//     * 获取用户名用于前端页面回显
//     * @return
//     */
//    @GetMapping("/me")
//    public ResponseEntity<UserDetail> queryUsername(HttpServletRequest request ) {
//
//        return ResponseEntity.ok(this.userService.queryUsername(request));
//    }



    /**
     * 获取用户名用于前端页面回显
     * @return
     */
    @GetMapping("/me")
    public ResponseEntity<UserDetail> queryUsername() {
        UserDetail userDetail = UserContext.getUser();
        return ResponseEntity.ok(userDetail);
    }


}
